package com.dminvoicemanager.adapters;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DM_CustomerList_Adapter {

	public static final String KEY_CUSROWID = "_id";
	public static final String KEY_CUSCODE = "DebCode";
	public static final String KEY_CUSNAME = "DebSumary";
	public static final String KEY_CUSTOWN = "TownCode";
	public static final String KEY_CUSAREA = "AreaCode";
	int c;
	private static final String TAG = "DM_CustomerList_Adapter";
	private DatabaseHelper dbHelperCustomer;
	private SQLiteDatabase mDb;

	private static final String DATABASE_NAME = "dminvoicemanager.db";
	private static final String SQLITE_TABLEDEB = "fDebtor";
	private static final int DATABASE_VERSION = 6;
	
	private final Context mCtx;

	private static final String DATABASE_CREATE = "CREATE TABLE if not exists "
			+ SQLITE_TABLEDEB + " (" + KEY_CUSROWID
			+ " integer PRIMARY KEY autoincrement," + KEY_CUSCODE + "," + KEY_CUSNAME
			+ "," + KEY_CUSTOWN + "," + KEY_CUSAREA + "," + " UNIQUE ("
			+ KEY_CUSCODE + "));";

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			/*Log.w(TAG, DATABASE_CREATE);
			db.execSQL(DATABASE_CREATE);*/
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
		/*	db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
			onCreate(db);*/
		}
	}

	public DM_CustomerList_Adapter(Context ctx) {
		this.mCtx = ctx;
	}

	public DM_CustomerList_Adapter open() throws SQLException {
		dbHelperCustomer = new DatabaseHelper(mCtx);
		mDb = dbHelperCustomer.getWritableDatabase();
		return this;
	}

	public void close() {
		if (dbHelperCustomer != null) {
			dbHelperCustomer.close();
		}
	}

	public long createCountry(String cuscode, String cusname, String cuscontinent,
			String cusregion) {

		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_CUSCODE, cuscode);
		initialValues.put(KEY_CUSNAME, cusname);
		initialValues.put(KEY_CUSTOWN, cuscontinent);
		initialValues.put(KEY_CUSAREA, cusregion);

		return mDb.insert(SQLITE_TABLEDEB, null, initialValues);
	}

	public boolean deleteAllCountries() {

		int doneDelete = 0;
		doneDelete = mDb.delete(SQLITE_TABLEDEB, null, null);
		Log.w(TAG, Integer.toString(doneDelete));
		return doneDelete > 0;

	}

	public Cursor fetchAllCustommersByName(String inputText) throws SQLException {
		Log.w(TAG, inputText);
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			mCursor = mDb.query(SQLITE_TABLEDEB, new String[] { KEY_CUSROWID,
					KEY_CUSCODE, KEY_CUSNAME, KEY_CUSTOWN, KEY_CUSAREA }, null,
					null, null, null, null);

		} else {
			mCursor = mDb.query(true, SQLITE_TABLEDEB, new String[] { KEY_CUSROWID,
					KEY_CUSCODE, KEY_CUSNAME, KEY_CUSTOWN, KEY_CUSAREA }, KEY_CUSNAME
					+ " like '%" + inputText + "%'", null, null, null, null,
					null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	public Cursor fetchAllCustommers() {
		c = c+1;
		Log.v(TAG, "DJ-Filter Customers :"+c);
		Cursor mCursor = mDb.query(SQLITE_TABLEDEB, new String[] { KEY_CUSROWID,
				KEY_CUSCODE, KEY_CUSNAME, KEY_CUSTOWN, KEY_CUSAREA }, null, null,
				null, null, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}

	

}
